<?php 

class GoodsListController extends VerifyController{
	public $model;
	public function __init(){
//		货品表
	    $this->model = K("Merchandise");
	}
	public function index(){
		$gid = Q('get.gid',0,'intval');
		//显示数据
		$merchandise = $this->model->where("goods_gid={$gid}")->all();
		foreach ($merchandise as $k => $v) {
			$merchandise[$k]['attrid'] = explode(',', $v['attrid']);
			foreach ($merchandise[$k]['attrid'] as $key => $val) {
				$flat = K('Specs')->where("specsattrid={$val}")->field("attrval")->find();
				$merchandise[$k]['attrid'][$key] = $flat['attrval'];
			}
			$this->assign("merchandise",$merchandise);
			
		}
		//添加数据
		$data = M()->join("__goods__ g join __typeattr__ t on g.tid=t.tid")->where("t.attrtype='规格' && gid={$gid}")->all();
		foreach ($data as $k => $v) {
			$tid= $v['attrid'];
			$data[$k]['spec'] = K("specs")->where("typeattrid={$tid} && gid={$gid}")->all();
		}
		$num = 1;
		$f = true;
		foreach ($data as $k => $v) {
			$num *= count($v['spec']);
		}
		
		$nn = $this->model->field('count(*) as nn')->where("goods_gid={$gid}")->find();
		if($num == $nn['nn'])$f=false;else $f=true;
		$this->assign('f',$f);
		$this->assign("data",$data);
	    $this->display();
	}
	public function edit(){
		$mid = Q('get.mid',0,'intval');
		if(IS_POST){
			$data = array(
			'stock' => $_POST['stock'],
		);
			$this->model->where("mid={$mid}")->update($data);
			$this->success("库存修改成功");
		}
	    $this->display();
	}
	public function add(){
	    if(!$this->model->addData()){
	    	$this->error($this->model->error);
	    }
		$this->success("添加货品成功");
	}
	public function del(){
	    $mid = Q("get.mid",0,"intval");
		if(!$this->model->where("mid={$mid}")->delete()){
			$this->error($this->model->error);
		}
		$this->success("删除成功");
		
	}
	
}
